package com.example.himalaya.view.adapters;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.example.himalaya.R;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PlayerTrackPagerAdapter extends PagerAdapter {

    private List<Track> mData = new ArrayList<>();

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.item_track_pager, container, false);

        // 将显示图片的控件放入
        container.addView(itemView);

        // get element
        // 设置显示的图片
        ImageView imageView = itemView.findViewById(R.id.track_pager_item);

        // 设置图片
        Track track = mData.get(position);

        String coverUrlLarge = track.getCoverUrlLarge();

        if (!TextUtils.isEmpty(coverUrlLarge)) {
            Picasso.with(container.getContext()).load(coverUrlLarge).into(imageView);
        } else {
            imageView.setImageResource(R.mipmap.default_picture);
        }


        return itemView;
    }


    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    public void setData(List<Track> list) {

        mData.clear();
        mData.addAll(list);
        notifyDataSetChanged();

    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

}
